unit uAppLog;

interface

uses
  SysUtils, Classes;

procedure WriteLog(const s: string);
procedure FmtWriteLog(const fmt: string; const args: array of const);
procedure LogException(const func: string; E: Exception);

implementation

uses
  SyncObjs, Windows, uDZSysUtils, uDZTrace, uDZLogWritter,
  uAppCmdLineParams;

var
  lock: TSynchroObject;
  trace: IDzTrace;

procedure LogException(const func: string; E: Exception);
begin
  WriteLog(Format('%s(%s): %s'#10, [func, E.ClassName, E.Message]));
end;

procedure WriteLog(const s: string);
begin
  lock.Acquire;
  try
    trace.Write(tsInformation, s);
  finally
    lock.Release;
  end;
end;

procedure FmtWriteLog(const fmt: string; const args: array of const);
begin
  lock.Acquire;
  try
    trace.FormatWrite(tsInformation, fmt, args);
  finally
    lock.Release;
  end;
end;

initialization
  lock := TCriticalSection.Create;
  if (SameText(cmdLineParams.LogType, 'Console')) then
  begin
    Windows.AllocConsole;
    trace := TConsoleTrace.Create;
  end
  else
    trace := TDzMultiFileLogWritter.Create(
      ExtractFilePath(ParamStr(0)) + 'logs');
  trace.Attributes['Encoding'] := teAnsi;
  trace.Attributes['LogSeparate'] := dtpHour;
  trace.DateTimeFormat := 'hh:nn:ss';
  trace.Options := [toTimeTick];
  
finalization
  lock.Free;

end.


